/*****************************************************************************\
Microsoft Research Asia
Copyright (c) Microsoft Corporation. All Rights Reserved.

Module Name:	ImagePos.cpp
Notes:				Implementation of Image Iterator.
History:			Created by Rong Xiao rxiao@microsoft.com		Jun 2, 2006
\*****************************************************************************/
#include "stdafx.h"
#include "ImagePos.h"
#include "ImageWrapper.h"

HRESULT CImagePos::Init ( const CImageWrapper* pImage )
{
	HRESULT hr=S_OK;
	if ( pImage==NULL)
	{
		hr = E_INVALIDARG; 
	}
	else 
	{
		m_pImage = pImage;
		m_uWidth=pImage->Width();
		m_uHeight=pImage->Height(); 
		m_uDstWidth = m_uWidth;
		m_uDstHeight = m_uHeight;
		InitIndex(0,0);
	}
	return hr;
}

void CScaledImagePos::InitScaledStep ()
{
	m_iScaledSx = Double2Int(((double)m_uWidth)/m_uDstWidth);
	m_iScaledSy = Double2Int(((double)m_uHeight)/m_uDstHeight);
	InitIndex(0,0);
}

HRESULT CScaledImagePos::Init(const CImageWrapper* pImage, unsigned uScaledWidth, unsigned uScaledHeight) 
{
	HRESULT hr=S_OK;
	if ( pImage==NULL)
	{
		hr = E_INVALIDARG; 
	}
	else 
	{
		CImagePos::Init(pImage);
		m_uDstWidth=uScaledWidth;
		m_uDstHeight=uScaledHeight; 
		InitScaledStep();
	}
	return hr;
}

HRESULT CScaledImagePos::Init(const CImageWrapper* pImage, double dResizeRatio)  
{
	HRESULT hr=S_OK;
	if ( pImage==NULL)
	{
		hr = E_INVALIDARG; 
	}
	else 
	{
		CImagePos::Init(pImage);

		double dAspect = ((double)m_uWidth)/m_uHeight;
		if ( m_uWidth > m_uHeight )
		{
			m_uDstHeight = (unsigned)(m_uHeight * dResizeRatio + 0.5);
			m_uDstWidth = (unsigned)(m_uDstHeight*dAspect + 0.5);
		}
		else
		{
			m_uDstWidth = (unsigned)(m_uWidth * dResizeRatio + 0.5);
			m_uDstHeight = (unsigned)(m_uDstWidth/dAspect + 0.5);
		}
		InitScaledStep();
	}
	return hr;
}


void CRotatedImagePos::InitRotatedStep(double dAngle)
{
	double dCos = cos(-dAngle);
	double dSin = sin(-dAngle);

	double sx = (double)m_uWidth / m_uDstWidth;
	double sy = (double)m_uHeight / m_uDstHeight;
	double dDU = ( (double) m_uWidth - m_uDstWidth * sx * dCos - m_uDstHeight * sy * dSin ) / 2;
	double dDV = ( (double) m_uHeight + m_uDstWidth * sx * dSin - m_uDstHeight * sy * dCos ) / 2;

	m_iScaledWSrc = Double2Int( m_uWidth);
	m_iScaledHSrc = Double2Int( m_uHeight);

	m_iScaledCosSx = Double2Int( sx * dCos);
	m_iScaledSinSx = Double2Int( sx * dSin);
	m_iScaledCosSy = Double2Int( sy * dCos);
	m_iScaledSinSy = Double2Int( sy * dSin);

	m_iScaledDU   =  Double2Int( dDU );
	m_iScaledDV   =  Double2Int( dDV );

	InitIndex(0,0);
}

HRESULT CRotatedImagePos::Init(const CImageWrapper* pImage, unsigned uScaledWidth, unsigned uScaledHeight, double dAngle) 
{
	HRESULT hr = CScaledImagePos::Init(pImage,uScaledWidth, uScaledHeight); 
	InitRotatedStep(dAngle);
	return hr;
}
HRESULT CRotatedImagePos::Init(const CImageWrapper* pImage, double dResizeRatio, double dAngle)  
{
	HRESULT hr = CScaledImagePos::Init(pImage,dResizeRatio);
	InitRotatedStep(dAngle);
	return hr;
}